package org.szgb.console.sys.user.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.sys.user.mapper.SysUserMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.TokenUtil;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysUserService {
    private final SysUserMapper sysUserMapper;

    @Autowired
    public SysUserService(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Transactional(readOnly = true)
    public SysUser getToken(SysUser user) {
        String token = user.getToken();
        if (token != null && !token.isEmpty()) {
            // 已登录，放行
            SysUser sysUser = new SysUser();
            sysUser.setId(TokenUtil.getUid(token));
            sysUser = this.get(sysUser.getId());
            sysUser.setToken(TokenUtil.getToken((sysUser.getId())));
            return sysUser;
        }
        SysUser sysUser = sysUserMapper.getOfLogin(user);
        if (sysUser == null) {
            return null;
        } else {
            sysUser.setToken(TokenUtil.getToken((sysUser.getId())));
            return sysUser;
        }
    }

    @Transactional(readOnly = true)
    public List<SysUser> fuzzyGet(String parameter) {
        return sysUserMapper.fuzzyGet(parameter);
    }

    @Transactional(readOnly = true)
    public SysUser get(String id) {
        return sysUserMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysUser> getUserInDepartmentWithRole(String departmentId, String roleId) {
        return sysUserMapper.getUserInDepartmentWithRole(departmentId, roleId);
    }

    @Transactional(readOnly = true)
    public List<SysUser> findList(SysUser sysUser) {
        if (sysUser != null && sysUser.getPageDto() != null) {
            Page<SysUser> page = new Page<>(sysUser.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysUser.setPage(page);
        }
        return sysUserMapper.findList(sysUser);
    }

    @Transactional(readOnly = true)
    public List<SysUser> getByIdList(String[] ids) {
        return sysUserMapper.getByIdArray(ids);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysUser sysUser) {
        return sysUserMapper.getCount(sysUser);
    }

    @Transactional()
    public void save(SysUser sysUser) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysUser.getId() == null || sysUser.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysUser.setCreatedBy(sysUser.getUpdatedBy());
            // 添加UUID
            sysUser.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysUserMapper.insert(sysUser);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysUserMapper.update(sysUser);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysUserMapper.delete(ids.split(","));
    }

    public SysUser getByIdAndPassword(String id,String password) {
        return sysUserMapper.getByIdAndPassword(id,password);
    }
}
